<?php

namespace app\index\controller;

use think\Db;

class StreetApply extends Base
{

    /**
     * 添加申请
     */
    public function addStreetApply()
    {
        $data = [];
        $res=0;
        input('cid') ? $data['cid'] = input('cid') : $this->err('缺少用户编码！');
        input('name') ? $data['name'] = input('name') : $this->err('缺少用户名！');
        input('mobile') ? $data['mobile'] = input('mobile') : $this->err('缺少手机号！');
        $data['createTime'] = createTime();

        $user = model('user')->where(['id' => $data['cid']])->find();

        if (empty($user)) {
            $this->err('用户编码错误！');
        }

        $streetApply = model('streetApply')->where(['cid' => input('cid')])->find();

        if(empty($streetApply)){
            $data['status']=0;
            $res = model('streetApply')->editData($data);
        }else{
            if($streetApply['status']==0){
                $this->err('已经申请过，请等待系统管理员审核！');
            }
            if($streetApply['status']==1){
                $this->err('您已经申请通过，无需再次申请！');
            }
            if($streetApply['status']==2){
                $data['status']=0;
                $res = model('streetApply')->where(['cid'=>input('cid')])->update($data);
            }
        }

        $res ? $this->suc($res) : $this->err('操作失败！');
    }

    /**
     * 验证是否申请
     */
    public function isStreetApply(){
        $where = [];
        input('cid') ? $where['cid'] = input('cid') : $this->err('缺少用户编码！');
        $user = model('user')->where(['id' => $where['cid']])->find();
        if (empty($user)) {
            $this->err('用户编码错误！');
        }
        $res = model('streetApply')->where(['cid' => input('cid')])->find();
        if($res==null){
            $res['status']=3;
        }
        $res ? $this->suc($res) : $this->err('验证失败！');
    }

}
